<?php


namespace app\admin\controller;


use app\common\model\Order;
use app\common\model\ServiceMiddle;

class Aftersales extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }


    /**
     * @route('aftersales/aftersaleslist')
     * 商品售后管理列表
     */
    public function aftersaleslist()
    {
        $isdisplay = true;
        $get['shop_name'] = input('get.shop_name');
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }

        $afterModel = new \app\common\model\Aftersales();
        $item = 'as.*,as.add_time as as_time,u.nick_name,u.avatar,s.shop_name,s.imagesurl,a.name,a.phone';
        $join = [
            ['ddt_user u','as.user_id = u.user_id'],
            ['ddt_shops s','as.shop_id = s.shop_id'],
            ['ddt_order o','as.order_id = o.order_id'],
            ['ddt_address a','o.address_id = a.address_id'],
        ];
        $rs_data = $afterModel->get_aftersales_list($get,$item,'as.state asc',$this->limit,$join,true);
//        var_dump($rs_data->toArray());die;
        $tvar = array(
            "title" => "申请售后管理",
            "time"  => time(),
            "data"  => $rs_data,
            "isdisplay" => $isdisplay,
            "get" => $get,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data->render());
        return $this->fetch('aftersales/aftersaleslist');
    }

    /**
     * @route('aftersales/aftersalesorder')
     * 已退款
     */
    public function aftersalesorder()
    {
        $OrderModel = new Order();
        $AfterModel = new \app\common\model\Aftersales();

        $order_id = input('order_id');

        $rs = $OrderModel->where('order_id', $order_id)->update(['order_status' => 6]);

        $rs1 = $AfterModel->where('order_id', $order_id)->update(['state' => 2,'update_time' => time()]);

        if($rs === false || $rs1 == false){
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }



    /**
     * @route('aftersales/servicelist')
     * 服务售后管理列表
     */
    public function servicelist()
    {

        $serviceMiddle = new ServiceMiddle();

        $cond['shop_id'] = $this->_shopid;
        $cond['states'] = "4,5";
        $field = "a.*,b.orderno,b.pay_amount,b.order_time,b.pay_time,c.service_name,c.price,c.sale_price,c.top_pic,d.shop_name,e.name,e.phone";
        $rs_data = $serviceMiddle->get_list_page($cond,'a.state asc',$field);
//        echo "<pre>";
//        var_dump($rs_data->toArray());
//        die;
        $tvar = array(
            "title" => "申请售后管理",
            "time"  => time(),
            "data"  => $rs_data,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data->render());
        return $this->fetch('aftersales/servicelist');
    }


    /**
     * @route('aftersales/aftersalesservice')
     * 已退款
     */
    public function aftersalesservice()
    {
        $OrderModel = new Order();
        $MiddleModel = new ServiceMiddle();
//        $AfterModel = new \app\common\model\Aftersales();

        $send_id = input('send_id');
        $send = $MiddleModel->getOne(['send_id' => $send_id])->toArray();

        $rs = $OrderModel->where('order_id', $send['order_id'])->update(['order_status' => 6]);

        $rs1 = $MiddleModel->where('send_id', $send_id)->update(['state' => 5]);
//        $rs1 = $AfterModel->where('order_id', $order_id)->update(['state' => 2]);

        if($rs === false || $rs1 == false){
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }



}